//
//  MoreFeedBack.m
//  Trading365
//
//  Created by Jason on 12-4-26.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ApproveShow.h"
#import "Type.h"
#import <QuartzCore/QuartzCore.h>
#import "TaskManager.h"
#import "XmlRpc.h"
#import "myToast.h"
#import "ApproveHisList.h"

static int btnHeight = 37;

@implementation ApproveShow
@synthesize content;
@synthesize bgButton;
@synthesize placeHolder;
@synthesize btnYES;
@synthesize btnNO;
@synthesize order_id,table_name;
@synthesize workflow_status;
@synthesize my_workflow_id;;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title=@"审批意见";
    
    self.view.backgroundColor=BACKGROUND_COLOR;
    self.content.frame = CGRectMake(15, 15, 290, 480-44-30-btnHeight);
    editRect = self.content.frame;
    btn1Rect = self.btnYES.frame;
    btn2Rect = self.btnNO.frame;
    
    self.btnYES.enabled=YES;
    self.btnNO.enabled=YES;
    send_type=0;

    [self.content setBackgroundColor:[UIColor clearColor]];
    
    [self.content.layer setBackgroundColor:[[UIColor whiteColor] CGColor]];
    [self.content.layer setBorderColor:[NAVIGATION_BAR_COLOR CGColor]];
    [self.content.layer setBorderWidth:1.0];
    [self.content.layer setCornerRadius:10.0];
    [self.content.layer setMasksToBounds:YES];
    self.content.clipsToBounds = YES;

    UIBarButtonItem *saveBtn = [[UIBarButtonItem alloc] 
                                initWithTitle:@"历史" 
                                style:UIBarButtonItemStylePlain
                                target:self 
                                action:@selector(history)]; 
    self.navigationItem.rightBarButtonItem = saveBtn;
    [saveBtn release];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidEndEditing:) name:UITextViewTextDidEndEditingNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidChange:) name:UITextViewTextDidChangeNotification object:nil];
    
    self.placeHolder.frame = CGRectMake(22, 19, 280, 30);
    self.placeHolder.hidden=NO;
    showPlaceHolder=YES;
    
}

- (void)viewDidUnload
{
    [self setContent:nil];
    [self setBgButton:nil];
    [self setPlaceHolder:nil];
    [self setBtnYES:nil];
    [self setBtnNO:nil];
        
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [content release];
    [bgButton release];
    [placeHolder release];
    [btnYES release];
    [btnNO release];
    [order_id release];
    [table_name release];
    [workflow_status release];
    [my_workflow_id release];
    [super dealloc];
}


#pragma mark - UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationDuration:0.30f];  
    editRect = self.content.frame;
    self.content.frame = CGRectMake(15, 15, 290, 480-44-20-10-216-60-btnHeight);
    self.btnYES.frame = CGRectMake(self.btnYES.frame.origin.x, 480-44-20-10-216-60 -btnHeight +15+10 ,self.btnYES.frame.size.width,self.btnYES.frame.size.height);
    self.btnNO.frame = CGRectMake(self.btnNO.frame.origin.x, 480-44-20-10-216-60 -btnHeight +15+10 ,self.btnNO.frame.size.width,self.btnNO.frame.size.height);
    
    [UIView commitAnimations];
    
    if (showPlaceHolder) {
        self.placeHolder.hidden=YES;
        showPlaceHolder=NO;
    }
    
    return;
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationDuration:0.30f];  
    self.content.frame = editRect;
    self.btnYES.frame = btn1Rect;
    self.btnNO.frame = btn2Rect;
    [UIView commitAnimations];
    return;
}

- (void)textViewDidChange:(UITextView *)textView
{
    if([self.content.text length] == 0)
    {
        self.placeHolder.hidden=NO;
        showPlaceHolder=YES;
    }else{
        self.placeHolder.hidden=YES;
        showPlaceHolder=NO;        
    }
}

#pragma mark -myfun
- (IBAction)approveNo:(id)sender {
    send_type =2;
    [self save];
}

- (IBAction)approveYes:(id)sender {
    send_type =1;
    [self save];
}

-(void)save
{
    NSString *alert=@"";
    
    if ([content.text length] < 1)
        alert=@"请填写审批意见.";
        
    if ([alert length]>0){
        UIAlertView *alertview=[[UIAlertView alloc] initWithTitle:@"提示" message:alert delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertview show];
        [alertview release];
        return;
    }    
    
    [self.content resignFirstResponder];
    
    XmlRpc *xml = [[XmlRpc alloc] init];
    NSMutableDictionary *tmp;

    NSLog(@"order_id=%@,workflow_status=%@,my_workflow_id=%@",order_id,workflow_status,my_workflow_id);
    
    tmp = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
           order_id,@"id",
           workflow_status,@"workflow_status",
           my_workflow_id,@"my_workflow_id",
           [NSString stringWithFormat:@"%d",send_type],@"send_type",
           content.text,@"audit",nil];
    
    NSLog(@"tmp=%@",tmp);
    
    [xml SendAgreeDisallow:tmp];
    
    NSString *msg=@"审批发送成功！";
    [myToast showWithText:msg];
    [[InfoType share] SetFlagFreshView:INFO_TYPE_VIEW_REFRESH];
        
    int xCount=[xml GetApproveListCount:[[Session share] getMemberRoleID] posiId:[[Session share] getMemberPosiID]];
    
    if (xCount > 0){
        [[Session share] setMsgCount5:xCount];
    }
    
    [tmp release];
    [xml release];
    
    self.btnYES.enabled=NO;
    self.btnNO.enabled=NO;
}

-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (IBAction)hideKeyBoard:(id)sender {
    [self.content resignFirstResponder];
    
    if (self.content.text.length < 1){
        self.placeHolder.hidden=NO;
        showPlaceHolder=YES;
    }
}

- (void) history
{
    ApproveHisList *history=[[ApproveHisList alloc] init];
    [history setTable_name:table_name];
    [history setOreder_id:order_id];
    [history setWorkflow_id:my_workflow_id];
    [self.navigationController pushViewController:history animated:YES];
    [history release];    
}

@end
